﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ThuVien;
namespace TranTekBE.NhaThuoc
{
    public partial class The_Extracare_Chua_Kich_Hoat : PageRequireLogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["sdt"] == null && Request.QueryString["store"] == null)
            {
                LoadData("", "");
            }
            else
            {
                LoadData(Request.QueryString["sdt"], Request.QueryString["store"]);
            }
            if (!IsPostBack)
            {
                
                // Get stores
                GetDropListStore(droplist_cua_hang_loc_kq);
                txt_sdt.Text = Request.QueryString["sdt"];
                droplist_cua_hang_loc_kq.SelectedValue = Request.QueryString["store"];
                //
            }
        }
        public static void GetDropListStore(DropDownList ddl)
        {
            DataSet ds = FunctionsUtils.Database.ExecuteQuery("select C007000 as IDCuaHang ,C007001 as CuaHang from T0070  where C007001 not in ('RACHMIEU','PMT')");
            FunctionsUtils.BindDataForControlList(ddl, ds, "CuaHang", "CuaHang", "", "Cửa hàng cố định");
        }
        void LoadData(string getsdt, string getstore)
        {
            string sql_loc = @"
                            select tichluy.C008102 as ThuocTinh,thanhvien.C003603 + ' ' + thanhvien.C003602 as KhachHang,
                            thanhvien.C003601 as MaSoThe, thanhvien.C003605 as SDT, thanhvien.C003617 as CuaHang from T0036 thanhvien
                            Left Join T0081 tichluy
                            on thanhvien.C003600 = tichluy.C008101
                            where tichluy.C008102 is null and thanhvien.C003601 not in ('') and thanhvien.C003601 is not null
                            ";
            string sql_sdt = "";
            string sql_stores = "";
            string sql_orderby = "order by thanhvien.C003601 asc";
            
            if (getsdt != "" && getsdt != null)
            {
                sql_sdt = " AND thanhvien.C003605 = '" + getsdt + "'";
            }

            if (getstore != "" && getstore != null)
            {
                sql_stores = " AND thanhvien.C003617 = '" + getstore + "'";
            }


            sql_loc = sql_loc + sql_sdt + sql_stores + sql_orderby;
            //
            DataSet ds = FunctionsUtils.Database.ExecuteQuery(sql_loc);

            DataTable dt = ds.Tables[0];

            PagedDataSource pgitems = new PagedDataSource();

            System.Data.DataView dv = new System.Data.DataView(dt);

            pgitems.DataSource = dv;

            pgitems.AllowPaging = true;

            pgitems.PageSize = 10;

            pgitems.CurrentPageIndex = PageNumber;

            if (pgitems.PageCount > 1)
            {

                rptPages.Visible = true;

                System.Collections.ArrayList pages = new System.Collections.ArrayList();

                for (int i = 0; i < pgitems.PageCount; i++)

                    pages.Add((i + 1).ToString());

                rptPages.DataSource = pages;

                rptPages.DataBind();

            }

            else

                rptPages.Visible = false;

            rpExtracare.DataSource = pgitems;

            rpExtracare.DataBind();

        }

        public int PageNumber
        {

            get
            {

                if (ViewState["PageNumber"] != null)

                    return Convert.ToInt32(ViewState["PageNumber"]);

                else

                    return 0;

            }

            set
            {

                ViewState["PageNumber"] = value;

            }

        }

        protected void rptPages_ItemCommand(object source, RepeaterCommandEventArgs e)
        {

            PageNumber = Convert.ToInt32(e.CommandArgument) - 1;

            if (Request.QueryString["sdt"] == null && Request.QueryString["store"] == null)
            {
                LoadData("", "");
            }
            else
            {
                LoadData(Request.QueryString["sdt"], Request.QueryString["store"]);
            }

        }

        protected void btnLoc_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["sdt"] == null && Request.QueryString["store"] == null)
            {
                LoadData("", "");
            }
            else
            {
                LoadData(Request.QueryString["sdt"], Request.QueryString["store"]);
            }
        }
        
        protected void txt_sdt_TextChanged(object sender, EventArgs e)
        {
            Response.Redirect("The-Extracare-Chua-Kich-Hoat.aspx?sdt=" + txt_sdt.Text.ToString() + "&store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString());
        }

        protected void droplist_cua_hang_loc_kq_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Redirect("The-Extracare-Chua-Kich-Hoat.aspx?sdt=" + txt_sdt.Text.ToString() + "&store=" + droplist_cua_hang_loc_kq.SelectedValue.ToString());
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            LoadData("", "");
            Response.Redirect("The-Extracare-Chua-Kich-Hoat.aspx");
        }

        
    }
}